package com.macro.mall.portal.controller.pay;

import com.alibaba.fastjson.JSON;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.pay.service.wechat.WeChatPayService;
import com.macro.mall.pay.service.wechat.bo.CreateH5PrePayInfoInput;
import com.macro.mall.pay.service.wechat.bo.CreateJsApiPrePayInfoInput;
import com.macro.mall.pay.service.wechat.bo.CreateJsApiPrePayInfoOutput;
import com.macro.mall.portal.controller.BaseController;
import com.macro.mall.portal.dto.pay.wechat.WeChatH5PayReqDto;
import com.macro.mall.portal.dto.pay.wechat.WeChatH5PayResResult;
import com.macro.mall.portal.dto.pay.wechat.WeChatJsApiPayReqDto;
import com.macro.mall.portal.dto.pay.wechat.WeChatJsApiPayResResult;
import com.macro.mall.portal.util.WebUtils;
import com.macro.mall.ums.enums.UserSourceTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信支付控制器
 *
 * @author xuyanjun
 * @date 2019-10-08
 */
@Api(tags = "WeChatPayController", description = "微信支付")
@RestController
@Slf4j(topic = "WeChatPayController")
public class WeChatPayController extends BaseController {

    @Autowired
    private WeChatPayService weChatPayService;

    /**
     * jsApi支付
     */
    @SuppressWarnings("Duplicates")
    @ApiOperation("微信jsApi支付")
    @PostMapping("/pay/wechat/jsApi")
    public CommonResult<WeChatJsApiPayResResult> jsApiPay(@RequestBody WeChatJsApiPayReqDto reqDto) {
        String notifyUrl = WebUtils.getRequestDomain() + "pay/callback/weChat/v1";
        log.error("支付回调地址:{}",notifyUrl);
        CreateJsApiPrePayInfoInput createJsApiPrePayInfoInput = new CreateJsApiPrePayInfoInput();
        createJsApiPrePayInfoInput
                .setSourceEnum(UserSourceTypeEnum.WX_MP)
                .setOrderId(reqDto.getOrderId())
                .setClientIp(WebUtils.getRemoteIp())
                .setCallbackNotifyUrl(notifyUrl);

        CreateJsApiPrePayInfoOutput jsApiPrePayInfo = weChatPayService.createJsApiPrePayInfo(createJsApiPrePayInfoInput);

        WeChatJsApiPayResResult result = new WeChatJsApiPayResResult();
        result.setNonceStr(jsApiPrePayInfo.getNonceStr())
                .setPackageValue(jsApiPrePayInfo.getPackageValue())
                .setPaySign(jsApiPrePayInfo.getPaySign())
                .setSignType(jsApiPrePayInfo.getSignType())
                .setTimeStamp(jsApiPrePayInfo.getTimeStamp());

        return CommonResult.success(result);
    }

    /**
     * H5支付
     */
    @ApiOperation("微信h5支付")
    @SuppressWarnings("Duplicates")
    @PostMapping("/pay/wechat/h5")
    public CommonResult<WeChatH5PayResResult> h5Pay(@RequestBody WeChatH5PayReqDto reqDto) {
        log.info("WeChatH5PayReqDto:{}", JSON.toJSONString(reqDto));

        CreateH5PrePayInfoInput createAppPrePayInfoInput = new CreateH5PrePayInfoInput();
        createAppPrePayInfoInput
                .setSourceEnum(UserSourceTypeEnum.WX_MP)
                .setOrderId(reqDto.getOrderId())
                .setClientIp(WebUtils.getRemoteIp())
                .setCallbackNotifyUrl(WebUtils.getRequestDomain() + "pay/callback/weChat/v1");

        String mwebUrl = weChatPayService.createH5PrePayInfo(createAppPrePayInfoInput);

        WeChatH5PayResResult result = new WeChatH5PayResResult();
        result.setMwebUrl(mwebUrl);

        return CommonResult.success(result);
    }


}
